/* $RCSFile: MediaPlayerLibDB.cs $
 * Apache license v2 2011 by Christian Surieux
 * $Source: ?/AddonNice/PageModules/MediaPlayerLib/MediaPlayerLibDB.cs $
 * $log$
 * Revision 19 2010/08/09 09:43:56  christian.surieux
 *   Added Log to cs sources files
 * Revision 6 2010/08/06 19:56:25  christian.surieux
 *   Added Log to All cs files
 */

using System;
using System.Diagnostics;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Globalization;

using AddonNice.Settings;
using AddonNice.Configuration;
using AddonNice.Diagnostics;

namespace AddonNice.Settings.PageModules.MediaPlayerLib
{
	/// <summary>
	/// Class that encapsulates all data logic necessary to add/query/delete
	/// MediaPlayerLib within the Portal database.
	/// </summary>
    public class MediaPlayerLibDB : DBDataStore
    {
		/// <summary>
		/// The GetMediaPlayerLib method returns a DataTable containing all of the
		/// MediaPlayerLib for a specific portal module from the MediaPlayerLib
		/// database table.
		/// </summary>
        public DataTable GetMediaPlayerLib(int moduleID) 
        {
            SqlDataAdapter myAdapt              =   new SqlDataAdapter("dbo.adn_GetMediaPlayerLib", myConnection);
            myAdapt.SelectCommand.CommandType   =   CommandType.StoredProcedure;

            AddModuleIDParameters(myAdapt.SelectCommand,moduleID);

			DataTable myDataTb					=   new DataTable("Medias");
			try
			{
				myAdapt.Fill(myDataTb);
			}
			catch(Exception exp)
			{
                if (DBTraceSwitch.Sw.TraceError)
				    Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[E]MediaPlayerLibDB GetMediaPlayerLib ex : {0}",exp),DBTraceSwitch.Sw.Info);
				throw;
			}
			finally
			{
				if ( myConnection.State == System.Data.ConnectionState.Open )
					myConnection.Close();
			}

			//  return the DataSet
			return CheckDataTableForDates(myDataTb);
		}

		/// <summary>
		/// The GetSingleMediaPlayerUrl method returns a SqlDataReader containing details
		/// about a specific MediaPlayerUrl from the MediaPlayerLib database table.
		/// </summary>
        public SqlDataReader GetSingleMediaPlayerUrl(int MDPID) 
        {
            SqlCommand myCommand            =   new SqlCommand("dbo.adn_GetSingleMediaPlayerUrl", myConnection);

            myCommand.CommandType           =   CommandType.StoredProcedure;

            SqlParameter parameterMDPID     =   new SqlParameter("@MDPID", SqlDbType.Int, 4);
            parameterMDPID.Value = MDPID;
            myCommand.Parameters.Add(parameterMDPID);

			SqlDataReader result			=   null;
			try
			{
				myConnection.Open();
				result						=   myCommand.ExecuteReader(CommandBehavior.CloseConnection);
			}
			catch(Exception exp)
			{
				if ( DBTraceSwitch.Sw.TraceError )
                    Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[E]MediaPlayerLibDB GetSingleMediaPlayerUrl MDPID : {0} \nex : {1}",MDPID,exp),DBTraceSwitch.Sw.Info);
				if ( result != null )
					result.Close();
				if ( myConnection.State == System.Data.ConnectionState.Open )
					myConnection.Close();
				throw;
			}
			return result;
        }

		public DataTable GetMediaPlayerLibGlobalized(int moduleID) 
		{
			SqlDataAdapter myAdapt              =   new SqlDataAdapter("dbo.adn_GetMediaPlayerLibGlobalized", myConnection);
			myAdapt.SelectCommand.CommandType   =   CommandType.StoredProcedure;

			AddGlobalizeParameters(myAdapt.SelectCommand);
            AddModuleIDParameters(myAdapt.SelectCommand,moduleID);

			DataTable myDataTb					=   new DataTable("Medias");
			try
			{
				myAdapt.Fill(myDataTb);
			}
			catch(Exception exp)
			{
				if ( DBTraceSwitch.Sw.TraceError )
                    Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[E]MediaPlayerLibDB GetMediaPlayerLibGlobalized ex : {0}",exp),DBTraceSwitch.Sw.Info);
				throw;
			}
			finally
			{
				if ( myConnection.State == System.Data.ConnectionState.Open )
					myConnection.Close();
			}

			//  return the DataSet
			return CheckDataTableForDates(myDataTb);
		}

        /// <summary>
		/// The GetSingleMediaPlayerUrl method returns a SqlDataReader containing details
		/// about a specific MediaPlayerUrl from the MediaPlayerLib database table.
		/// </summary>
		public SqlDataReader GetSingleMediaPlayerUrlGlobalized(int MDPID) 
		{
			SqlCommand myCommand        =   new SqlCommand("dbo.adn_GetSingleMediaPlayerUrlGlobalized", myConnection);
			myCommand.CommandType       =   CommandType.StoredProcedure;

			AddGlobalizeParameters(myCommand);

			SqlParameter parameterMDPID =   new SqlParameter("@MDPID", SqlDbType.Int, 4);
			parameterMDPID.Value        =   MDPID;
			myCommand.Parameters.Add(parameterMDPID);

			SqlDataReader result		=   null;
			try
			{
				myConnection.Open();
				result					=   myCommand.ExecuteReader(CommandBehavior.CloseConnection);
			}
			catch(Exception exp)
			{
				if ( DBTraceSwitch.Sw.TraceError )
                    Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[E]MediaPlayerLibDB GetSingleMediaPlayerUrlGlobalized MDPID: {0} ex: {1}",MDPID,exp),DBTraceSwitch.Sw.Info);
				if ( result != null )
					result.Close();
				if ( myConnection.State == System.Data.ConnectionState.Open )
					myConnection.Close();
				throw;
			}
			return result;
		}

        /// <summary>
		/// The DeleteMediaPlayerUrl method deletes the specified MediaPlayerUrl from
		/// the MediaPlayerLib database table.
		/// </summary>
        public void DeleteMediaPlayerUrl(int MDPID) 
        {
            SqlCommand myCommand        =   new SqlCommand("dbo.adn_DeleteMediaPlayerUrl", myConnection);

            myCommand.CommandType       =   CommandType.StoredProcedure;

            SqlParameter parameterMDPID =   new SqlParameter("@MDPID", SqlDbType.Int, 4);
            parameterMDPID.Value        =   MDPID;
            myCommand.Parameters.Add(parameterMDPID);

			try
			{
				myConnection.Open();
				myCommand.ExecuteNonQuery();
			}
			catch(Exception exp)
			{
				if ( DBTraceSwitch.Sw.TraceError )
                    Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[E]MediaPlayerLibDB DeleteMediaPlayerUrl  MDPID: {0} ex: {1}",MDPID,exp),DBTraceSwitch.Sw.Info);
				throw;
			}
			finally
			{
				if ( myConnection.State == System.Data.ConnectionState.Open )
					myConnection.Close();
			}
		}

        /// <summary>
        /// The AddMediaPlayerUrl method adds a new MediaPlayerUrl to the
        /// MediaPlayerLib database table, and returns the MDPID value as a result.
        /// </summary>
        public int AddMediaPlayerUrl(int moduleID, int MDPID,string category,int MDPLength,string MDPType,int Type2,
            string MDPTitle, string MDPDescription,string MDPUrl) 
        {
            SqlCommand myCommand                =   new SqlCommand("dbo.adn_AddMediaPlayerUrl", myConnection);
            myCommand.CommandType               =   CommandType.StoredProcedure;
            
            SqlParameter parameterMDPID         =   new SqlParameter("@MDPID", SqlDbType.Int, 4);
            parameterMDPID.Direction            =   ParameterDirection.Output;
            myCommand.Parameters.Add(parameterMDPID);

            AddModuleIDParameters(myCommand,moduleID);
            
            SqlParameter parameterMDPCategory   =   new SqlParameter("@MDPCategory", SqlDbType.NVarChar, 50);
            parameterMDPCategory.Value          =   category;
            myCommand.Parameters.Add(parameterMDPCategory);

            SqlParameter parameterMDPLength     =   new SqlParameter("@MDPLength", SqlDbType.Int);
            parameterMDPLength.Value            =   MDPLength;
            myCommand.Parameters.Add(parameterMDPLength);

            SqlParameter parameterMDPType       =   new SqlParameter("@MDPType", SqlDbType.NVarChar, 10);
            parameterMDPType.Value              =   MDPType;
            myCommand.Parameters.Add(parameterMDPType);

            SqlParameter parameterMDPType2      =   new SqlParameter("@MDPType2", SqlDbType.Int);
            parameterMDPType2.Value             =   Type2;
            myCommand.Parameters.Add(parameterMDPType2);

            SqlParameter parameterMDPTitle      =   new SqlParameter("@MDPTitle", SqlDbType.NVarChar, 150);
            parameterMDPTitle.Value             =   MDPTitle;
            myCommand.Parameters.Add(parameterMDPTitle);

            SqlParameter parameterMDPUrl        =   new SqlParameter("@MDPUrl", SqlDbType.NVarChar, 150);
            parameterMDPUrl.Value               =   MDPUrl;
            myCommand.Parameters.Add(parameterMDPUrl);

            SqlParameter parameterMDPDescription=   new SqlParameter("@MDPDescription", SqlDbType.NVarChar, 2000);
            parameterMDPDescription.Value       =   MDPDescription;
            myCommand.Parameters.Add(parameterMDPDescription);

			int ret							    =	-1;
			try
			{
				myConnection.Open();
				myCommand.ExecuteNonQuery();
				ret			=	(int)parameterMDPID.Value;
			}
			catch(Exception exp)
			{
				if ( DBTraceSwitch.Sw.TraceError )
                    Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[E]MediaPlayerLibDB AddMediaPlayerUrl  moduleID: {0} ex: {1}",moduleID,exp),DBTraceSwitch.Sw.Info);
				throw;
			}
			finally
			{
				if ( myConnection.State == System.Data.ConnectionState.Open )
					myConnection.Close();
			}
			return ret;
        }

        /// <summary>
        /// The UpdateMediaPlayerUrl method updates the specified MediaPlayerUrl within
        /// the MediaPlayerLib database table.
        /// </summary>
        public void UpdateMediaPlayerUrl(int moduleID, int MDPID,string category,int MDPLength,string MDPType,int Type2,
            string MDPTitle, string MDPDescription,string MDPUrl) 
        {
            SqlCommand myCommand                =   new SqlCommand("dbo.adn_UpdateMediaPlayerUrl", myConnection);

            myCommand.CommandType               =   CommandType.StoredProcedure;

            SqlParameter parameterMDPID         =   new SqlParameter("@MDPID", SqlDbType.Int, 4);
            parameterMDPID.Value                =   MDPID;
            myCommand.Parameters.Add(parameterMDPID);

            SqlParameter parameterMDPCategory   =   new SqlParameter("@MDPCategory", SqlDbType.NVarChar, 50);
            parameterMDPCategory.Value          =   category;
            myCommand.Parameters.Add(parameterMDPCategory);

            SqlParameter parameterMDPLength     =   new SqlParameter("@MDPLength", SqlDbType.Int);
            parameterMDPLength.Value            =   MDPLength;
            myCommand.Parameters.Add(parameterMDPLength);

            SqlParameter parameterMDPType       =   new SqlParameter("@MDPType", SqlDbType.NVarChar, 10);
            parameterMDPType.Value              =   MDPType;
            myCommand.Parameters.Add(parameterMDPType);

            SqlParameter parameterMDPType2      =   new SqlParameter("@MDPType2", SqlDbType.Int);
            parameterMDPType2.Value             =   Type2;
            myCommand.Parameters.Add(parameterMDPType2);

            SqlParameter parameterMDPTitle      =   new SqlParameter("@MDPTitle", SqlDbType.NVarChar, 150);
            parameterMDPTitle.Value             =   MDPTitle;
            myCommand.Parameters.Add(parameterMDPTitle);

            SqlParameter parameterMDPUrl        =   new SqlParameter("@MDPUrl", SqlDbType.NVarChar, 150);
            parameterMDPUrl.Value               =   MDPUrl;
            myCommand.Parameters.Add(parameterMDPUrl);

            SqlParameter parameterMDPDescription =  new SqlParameter("@MDPDescription", SqlDbType.NVarChar, 2000);
            parameterMDPDescription.Value       =   MDPDescription;
            myCommand.Parameters.Add(parameterMDPDescription);

			try
			{
				myConnection.Open();
				myCommand.ExecuteNonQuery();
			}
			catch(Exception exp)
			{
				if ( DBTraceSwitch.Sw.TraceError )
                    Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[E]MediaPlayerLibDB UpdateMediaPlayerUrl  MDPID:{0} nex : {1}",MDPID,exp),DBTraceSwitch.Sw.Info);
				throw;
			}
			finally
			{
				if ( myConnection.State == System.Data.ConnectionState.Open )
					myConnection.Close();
			}
		}
	
		public void DeleteMediaPlayerUrlGlobalized(int MDPID) 
		{
			SqlCommand myCommand                =   new SqlCommand("dbo.adn_DeleteMediaPlayerUrlGlobalized", myConnection);
			myCommand.CommandType               =   CommandType.StoredProcedure;

			SqlParameter parameterMDPID         =   new SqlParameter("@MDPID", SqlDbType.Int, 4);
			parameterMDPID.Value                =   MDPID;
			myCommand.Parameters.Add(parameterMDPID);

			try
			{
				myConnection.Open();
				myCommand.ExecuteNonQuery();
			}
			catch(Exception exp)
			{
				if ( DBTraceSwitch.Sw.TraceError )
                    Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[E]MediaPlayerLibDB DeleteMediaPlayerUrlGlobalized  MDPID :{0}, ex: {1}",MDPID, exp),DBTraceSwitch.Sw.Info);
				throw;
			}
			finally
			{
				if ( myConnection.State == System.Data.ConnectionState.Open )
					myConnection.Close();
			}
		}

        
		public DataTable GetMediaPlayerCategoriesGlobalized(int moduleID) 
		{
			SqlDataAdapter myAdapt              =   new SqlDataAdapter("dbo.adn_GetMediaPlayerCategoriesGlobalized", myConnection);
			myAdapt.SelectCommand.CommandType   =   CommandType.StoredProcedure;

			AddGlobalizeParameters(myAdapt.SelectCommand);
            AddModuleIDParameters(myAdapt.SelectCommand,moduleID);

			DataTable myDataTb					=   new DataTable("Medias");
			try
			{
				myAdapt.Fill(myDataTb);
			}
			catch(Exception exp)
			{
				if ( DBTraceSwitch.Sw.TraceError )
                    Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[E]MediaPlayerLibDB GetMediaPlayerCategoriesGlobalized ex: {0}",exp),DBTraceSwitch.Sw.Info);
				throw;
			}
			finally
			{
				if ( myConnection.State == System.Data.ConnectionState.Open )
					myConnection.Close();
			}

			return myDataTb;
		}

        public void UpdateMediaPlayerUrlGlobalized(int moduleID, int MDPID,string category,int MDPLength,string MDPType,int Type2,
            string MDPTitle, string MDPDescription,string MDPUrl) 
		{
			SqlCommand myCommand				    =   new SqlCommand("dbo.adn_UpdateMediaPlayerUrlGlobalized", myConnection);
			myCommand.CommandType				    =   CommandType.StoredProcedure;

			AddGlobalizeParameters(myCommand);

			SqlParameter parameterMDPID             =   new SqlParameter("@MDPID", SqlDbType.Int, 4);
			parameterMDPID.Value                    =   MDPID;
			myCommand.Parameters.Add(parameterMDPID);

            AddModuleIDParameters(myCommand,moduleID);

            SqlParameter parameterMDPCategory       =   new SqlParameter("@MDPCategory", SqlDbType.NVarChar, 50);
            parameterMDPCategory.Value              =   category;
            myCommand.Parameters.Add(parameterMDPCategory);

            SqlParameter parameterMDPLength         =   new SqlParameter("@MDPLength", SqlDbType.Int);
            parameterMDPLength.Value                =   MDPLength;
            myCommand.Parameters.Add(parameterMDPLength);

            SqlParameter parameterMDPType           =   new SqlParameter("@MDPType", SqlDbType.NVarChar, 10);
            parameterMDPType.Value                  =   MDPType;
            myCommand.Parameters.Add(parameterMDPType);

            SqlParameter parameterMDPType2          =   new SqlParameter("@MDPType2", SqlDbType.Int);
            parameterMDPType2.Value                 =   Type2;
            myCommand.Parameters.Add(parameterMDPType2);

            SqlParameter parameterMDPTitle          =   new SqlParameter("@MDPTitle", SqlDbType.NVarChar, 150);
			parameterMDPTitle.Value                 =   MDPTitle;
			myCommand.Parameters.Add(parameterMDPTitle);

			SqlParameter parameterMDPUrl            =   new SqlParameter("@MDPUrl", SqlDbType.NVarChar, 150);
			parameterMDPUrl.Value                   =   MDPUrl;
			myCommand.Parameters.Add(parameterMDPUrl);

			SqlParameter parameterMDPDescription    =   new SqlParameter("@MDPDescription", SqlDbType.NVarChar, 2000);
			parameterMDPDescription.Value           =   MDPDescription;
			myCommand.Parameters.Add(parameterMDPDescription);

			try
			{
				myConnection.Open();
				myCommand.ExecuteNonQuery();
			}
			catch(Exception exp)
			{
				if ( DBTraceSwitch.Sw.TraceError )
                    Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[E]MediaPlayerLibDB UpdateMediaPlayerUrlGlobalized  MDPID: {0} ex: {1}",MDPID,exp),DBTraceSwitch.Sw.Info);
				throw;
			}
			finally
			{
				if ( myConnection.State == System.Data.ConnectionState.Open )
					myConnection.Close();
			}
		}
	
	}
}
